(* Content-type: application/mathematica *)

(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)

(* CreatedBy='Mathematica 7.0' *)

(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[       145,          7]
NotebookDataLength[     16803,        344]
NotebookOptionsPosition[     16545,        331]
NotebookOutlinePosition[     16889,        346]
CellTagsIndexPosition[     16846,        343]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{
Cell[BoxData[
 RowBox[{"\[IndentingNewLine]", 
  RowBox[{
   RowBox[{"Directory", "[", "]"}], "\[IndentingNewLine]", 
   RowBox[{"K", "=", "1"}], "\n", 
   RowBox[{
    RowBox[{"P", " ", "=", " ", 
     RowBox[{
      RowBox[{"(", 
       RowBox[{"2", "*", "K"}], ")"}], " ", "+", " ", "1"}]}], ";"}], "\n", 
   RowBox[{
    RowBox[{"Np", " ", "=", " ", 
     RowBox[{"(", 
      RowBox[{"P", " ", "+", " ", "1"}], ")"}]}], ";"}], "\n", 
   RowBox[{
    RowBox[{"Dk", " ", "=", " ", 
     RowBox[{"(", 
      RowBox[{"K", " ", "+", " ", "1"}], ")"}]}], ";"}], 
   "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"xCoords", " ", "=", " ", 
     RowBox[{"{", 
      RowBox[{
       RowBox[{"-", "1"}], ",", " ", "1"}], "}"}]}], ";"}], 
   "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"fname", " ", "=", " ", 
     RowBox[{"StringJoin", "[", 
      RowBox[{"{", 
       RowBox[{"\"\<C\>\"", ",", " ", 
        RowBox[{"IntegerString", "[", "K", "]"}], ",", "  ", 
        "\"\<ShFnCoeffs1D.txt\>\""}], "}"}], "]"}]}], ";"}], 
   "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"fp", " ", "=", " ", 
     RowBox[{"OpenWrite", "[", "fname", "]"}]}], ";"}], "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"maxResNorm", " ", "=", " ", 
     RowBox[{"-", "1.0"}]}], ";"}], "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"mat", " ", "=", " ", 
     RowBox[{"Table", "[", 
      RowBox[{"0", ",", 
       RowBox[{"{", "Np", "}"}], ",", 
       RowBox[{"{", " ", "Np", "}"}]}], "]"}]}], ";"}], "        ", 
   "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"For", "[", " ", 
     RowBox[{
      RowBox[{"nd", " ", "=", " ", "1"}], ",", 
      RowBox[{"nd", "\[LessEqual]", " ", "2"}], ",", " ", 
      RowBox[{"nd", "++"}], ",", "\[IndentingNewLine]", "      ", 
      RowBox[{
       RowBox[{"x", " ", "=", " ", 
        RowBox[{"xCoords", "[", 
         RowBox[{"[", "nd", "]"}], "]"}]}], ";", "\[IndentingNewLine]", 
       "                    ", 
       RowBox[{"For", "[", " ", 
        RowBox[{
         RowBox[{"xs", " ", "=", " ", "1"}], ",", 
         RowBox[{"xs", "\[LessEqual]", 
          RowBox[{"(", 
           RowBox[{"K", " ", "+", " ", "1"}], ")"}]}], ",", " ", 
         RowBox[{"xs", "++"}], ",", "\[IndentingNewLine]", 
         "                        ", 
         RowBox[{
          RowBox[{"rowId", " ", "=", " ", 
           RowBox[{
            RowBox[{"(", 
             RowBox[{"Dk", "*", 
              RowBox[{"(", 
               RowBox[{"nd", " ", "-", " ", "1"}], ")"}]}], ")"}], " ", "+", 
            " ", "xs"}]}], ";", "\[IndentingNewLine]", 
          "                                ", 
          RowBox[{"For", "[", " ", 
           RowBox[{
            RowBox[{"xc", " ", "=", " ", "1"}], ",", 
            RowBox[{"xc", "\[LessEqual]", " ", 
             RowBox[{"(", 
              RowBox[{"P", " ", "+", " ", "1"}], ")"}]}], ",", 
            RowBox[{"xc", "++"}], ",", "\[IndentingNewLine]", 
            "                                    ", 
            RowBox[{
             RowBox[{"colId", " ", "=", " ", "xc"}], ";", 
             "\[IndentingNewLine]", "                                    ", 
             RowBox[{"fac", " ", "=", " ", "1"}], ";", "\[IndentingNewLine]", 
             "                                     ", 
             RowBox[{"xVal", " ", "=", " ", "0"}], ";", 
             "                                     ", "\[IndentingNewLine]", 
             RowBox[{"If", "[", 
              RowBox[{
               RowBox[{"xc", " ", "\[GreaterEqual]", "  ", "xs"}], ",", 
               "\[IndentingNewLine]", "   ", 
               RowBox[{
                RowBox[{"xVal", " ", "=", " ", 
                 SuperscriptBox["x", 
                  RowBox[{"(", 
                   RowBox[{"xc", " ", "-", " ", "xs"}], ")"}]]}], ";", 
                "\[IndentingNewLine]", " ", 
                RowBox[{"For", "[", " ", 
                 RowBox[{
                  RowBox[{"i", " ", "=", " ", "1"}], ",", 
                  RowBox[{"i", "\[LessEqual]", 
                   RowBox[{"(", 
                    RowBox[{"xs", " ", "-", " ", "1"}], ")"}]}], ",", 
                  RowBox[{"i", "++"}], ",", " ", 
                  RowBox[{"fac", " ", "=", " ", 
                   RowBox[{"fac", "*", 
                    RowBox[{"(", 
                    RowBox[{"xc", " ", "-", " ", "i"}], ")"}]}]}]}], "]"}], 
                ";"}]}], "\[IndentingNewLine]", "]"}], ";", 
             "\[IndentingNewLine]", "                                 ", 
             RowBox[{
              RowBox[{"mat", "[", 
               RowBox[{"[", 
                RowBox[{"rowId", ",", " ", "colId"}], "]"}], "]"}], " ", "=", 
              " ", 
              RowBox[{"(", 
               RowBox[{"fac", "*", "xVal"}], ")"}]}], ";"}]}], 
           "\[IndentingNewLine]", "                                ", "]"}], 
          ";"}]}], "\[IndentingNewLine]", "            ", "]"}], ";"}]}], 
     "\[IndentingNewLine]", "        ", "]"}], ";"}], "\[IndentingNewLine]", 
   "\[IndentingNewLine]", "        ", 
   RowBox[{
    RowBox[{"For", "[", " ", 
     RowBox[{
      RowBox[{"row", " ", "=", " ", "1"}], ",", 
      RowBox[{"row", "\[LessEqual]", " ", "Np"}], ",", " ", 
      RowBox[{"row", "++"}], ",", "\[IndentingNewLine]", "            ", 
      RowBox[{
       RowBox[{"rhs", " ", "=", " ", 
        RowBox[{"Table", "[", 
         RowBox[{"0", ",", 
          RowBox[{"{", 
           RowBox[{"ii", ",", "Np"}], "}"}]}], "]"}]}], ";", 
       "\[IndentingNewLine]", "            ", 
       RowBox[{
        RowBox[{"rhs", "[", 
         RowBox[{"[", "row", "]"}], "]"}], " ", "=", " ", "1"}], ";", 
       "\[IndentingNewLine]", "          ", 
       RowBox[{"sol", "=", 
        RowBox[{"LinearSolve", "[", 
         RowBox[{"mat", ",", "rhs"}], "]"}]}], ";", "\[IndentingNewLine]", 
       "          ", 
       RowBox[{"res", "=", 
        RowBox[{"rhs", "-", 
         RowBox[{"(", 
          RowBox[{"mat", ".", "sol"}], ")"}]}]}], ";", "\[IndentingNewLine]", 
       RowBox[{"resNorm", "=", 
        RowBox[{"Norm", "[", 
         RowBox[{"res", ",", "Infinity"}], "]"}]}], ";", 
       "\[IndentingNewLine]", "          ", 
       RowBox[{"If", "[", 
        RowBox[{
         RowBox[{"resNorm", " ", ">", " ", "maxResNorm"}], ",", 
         RowBox[{"maxResNorm", " ", "=", " ", "resNorm"}]}], " ", "]"}], ";", 
       "\[IndentingNewLine]", 
       RowBox[{"If", "[", 
        RowBox[{
         RowBox[{"resNorm", " ", ">", " ", 
          RowBox[{
           RowBox[{"1.0", "e"}], "-", "14"}]}], ",", "  ", 
         "\[IndentingNewLine]", "   ", 
         RowBox[{
          RowBox[{"Print", "[", "\"\<Did not solve exactly!\>\"", "]"}], ";", 
          "\[IndentingNewLine]", "                ", 
          RowBox[{"Close", "[", "fp", "]"}], ";", "\[IndentingNewLine]", 
          "                ", 
          RowBox[{"Abort", "[", "]"}], ";"}]}], "\[IndentingNewLine]", 
        "            ", "]"}], ";", "\n", "            ", 
       RowBox[{"For", "[", " ", 
        RowBox[{
         RowBox[{"col", " ", "=", " ", "1"}], ",", 
         RowBox[{"col", "\[LessEqual]", " ", "Np"}], ",", " ", 
         RowBox[{"col", "++"}], ",", "\[IndentingNewLine]", "  ", 
         RowBox[{
          RowBox[{"ratForm", " ", "=", " ", 
           RowBox[{"Rationalize", "[", 
            RowBox[{
             RowBox[{"sol", "[", 
              RowBox[{"[", "col", "]"}], "]"}], ",", " ", "0"}], "]"}]}], ";",
           "\[IndentingNewLine]", 
          RowBox[{"numer", " ", "=", " ", 
           RowBox[{"Numerator", "[", "ratForm", "]"}]}], ";", 
          "\[IndentingNewLine]", 
          RowBox[{"denom", " ", "=", " ", 
           RowBox[{"Denominator", "[", "ratForm", "]"}]}], ";", 
          "\[IndentingNewLine]", 
          RowBox[{"Print", "[", "ratForm", "]"}], ";", "\[IndentingNewLine]", 
          RowBox[{"Write", "[", 
           RowBox[{"fp", ",", "numer"}], " ", "]"}], ";", 
          "\[IndentingNewLine]", 
          RowBox[{"Write", "[", 
           RowBox[{"fp", ",", " ", "denom"}], "]"}]}]}], 
        "\[IndentingNewLine]", "]"}], ";"}]}], "        ", 
     "\[IndentingNewLine]", "]"}], ";"}], "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"Close", "[", "fp", "]"}], ";"}], " ", "       ", 
   "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"Print", "[", 
     RowBox[{"StringJoin", "[", 
      RowBox[{"{", 
       RowBox[{"\"\<MaxResNorm: \>\"", ",", 
        RowBox[{"ToString", "[", "maxResNorm", "]"}]}], "}"}], " ", "]"}], 
     " ", "]"}], ";"}], "\[IndentingNewLine]", 
   RowBox[{"Print", "[", "\"\<Done\>\"", "]"}]}]}]], "Input",
 CellChangeTimes->{{3.514304614342812*^9, 3.5143046462056255`*^9}, {
   3.514306028109801*^9, 3.5143061518411727`*^9}, {3.514306689247908*^9, 
   3.5143066920011835`*^9}, {3.514306725378521*^9, 3.514306736702653*^9}, {
   3.514307056364616*^9, 3.5143071386088395`*^9}, {3.514307173987377*^9, 
   3.514307214089387*^9}, {3.514307360182995*^9, 3.514307402397216*^9}, 
   3.5143074616831436`*^9, {3.5143075513171062`*^9, 3.5143075709200664`*^9}, {
   3.514307602784252*^9, 3.514307651099084*^9}, {3.514307684774451*^9, 
   3.514307689889962*^9}, {3.514307769301903*^9, 3.514307991019072*^9}, {
   3.514308060534023*^9, 3.5143084412210875`*^9}, {3.5143084741460123`*^9, 
   3.5143087404953747`*^9}, {3.5143087773950973`*^9, 
   3.5143089949790974`*^9}, {3.5143090580000973`*^9, 3.514309116364097*^9}, {
   3.514309383586133*^9, 3.51430943266504*^9}, {3.5143126903717785`*^9, 
   3.5143126955913*^9}, {3.5143129620919476`*^9, 3.5143130066554036`*^9}, {
   3.5143133283785725`*^9, 3.514313355994334*^9}, {3.5143135079915323`*^9, 
   3.514313512624995*^9}, {3.5143135527710094`*^9, 3.5143136102207537`*^9}, {
   3.514313670843816*^9, 3.514313704319163*^9}, {3.5143139388506136`*^9, 
   3.5143139535410824`*^9}, {3.514314149556682*^9, 3.5143141586095877`*^9}, 
   3.514314199769703*^9, {3.5143142663343587`*^9, 3.514314267055431*^9}, {
   3.514314363877112*^9, 3.5143144089506187`*^9}, {3.5143145203687596`*^9, 
   3.514314526352358*^9}, {3.514314609127635*^9, 3.514314650904812*^9}, {
   3.514314749053626*^9, 3.5143147516458855`*^9}, 3.514314807309451*^9, {
   3.5143148706937885`*^9, 3.5143148913848577`*^9}, {3.5143149261773367`*^9, 
   3.514314938826601*^9}, {3.514314989110629*^9, 3.5143150418219*^9}, {
   3.5143151359583125`*^9, 3.5143151383625526`*^9}, {3.5143151748352*^9, 
   3.5143152054872646`*^9}, {3.514315289086624*^9, 3.5143153207067857`*^9}, {
   3.514315358534568*^9, 3.514315415789293*^9}, {3.5143154596556787`*^9, 
   3.514315500309744*^9}, {3.5143768236070004`*^9, 3.5143768392390003`*^9}, 
   3.51437693337*^9, {3.514377117006*^9, 3.51437712192*^9}, {
   3.5143772748529997`*^9, 3.514377285594*^9}, {3.514377398414*^9, 
   3.514377409859*^9}, {3.514377493059*^9, 3.514377516109*^9}, 
   3.514377717361*^9, {3.514377827396*^9, 3.514377907777*^9}, {
   3.514378004382*^9, 3.514378036667*^9}, {3.514378409297*^9, 
   3.514378421295*^9}, {3.514378458468*^9, 3.514378620973*^9}, {
   3.514378734976*^9, 3.5143787387349997`*^9}, {3.51437922751*^9, 
   3.514379236882*^9}, {3.5143794986*^9, 3.514379523366*^9}, {
   3.5143801208269997`*^9, 3.51438013256*^9}, {3.514380178881*^9, 
   3.514380303693*^9}, {3.514380793891*^9, 3.514380804488*^9}, {
   3.5143812526689997`*^9, 3.5143812607860003`*^9}, {3.514382293817*^9, 
   3.514382297509*^9}, 3.514382558097*^9, {3.514382940797*^9, 
   3.5143829567130003`*^9}, {3.514383009744*^9, 3.514383011517*^9}, {
   3.514383081547*^9, 3.514383082375*^9}, {3.514383499741*^9, 
   3.5143835013719997`*^9}, {3.514383590031*^9, 3.51438361395*^9}, {
   3.514383648027*^9, 3.514383649115*^9}, {3.514384185333*^9, 
   3.514384192818*^9}, {3.514545124887729*^9, 3.514545217556678*^9}, {
   3.5145452647653985`*^9, 3.5145453101099324`*^9}, {3.514545429288849*^9, 
   3.5145454844550934`*^9}, {3.5145456158860936`*^9, 
   3.5145456362820935`*^9}, {3.5145457277780933`*^9, 
   3.5145457409000936`*^9}, {3.5145457753790936`*^9, 
   3.5145457798380938`*^9}, {3.5145458841014404`*^9, 
   3.5145459624301033`*^9}, {3.514546028613337*^9, 3.514546036748964*^9}, {
   3.514546073847382*^9, 3.514546147446099*^9}, {3.514546375893437*^9, 
   3.5145463833691845`*^9}, {3.5145464337222195`*^9, 
   3.5145464780650883`*^9}, {3.5145465626460886`*^9, 
   3.5145466012470884`*^9}, {3.5145466339000883`*^9, 
   3.5145466373980885`*^9}, {3.514546733015088*^9, 3.5145467361080885`*^9}, {
   3.5145467960440884`*^9, 3.514546799272088*^9}, {3.5145469756252747`*^9, 
   3.5145470180947666`*^9}, 3.5145472568964443`*^9, {3.5145479030192223`*^9, 
   3.514547904929222*^9}, 3.514551563152522*^9, {3.514555963083563*^9, 
   3.514555968900726*^9}, 3.514558390226759*^9, {3.5145588250772395`*^9, 
   3.5145588317499065`*^9}, {3.5145592147041984`*^9, 3.514559218376566*^9}, 
   3.514559362122939*^9, 3.514559435509277*^9, {3.5145595893916636`*^9, 
   3.5145596136190863`*^9}, {3.514559682594983*^9, 3.5145596905617795`*^9}, 
   3.5145597425269756`*^9, {3.5145598732790337`*^9, 3.514559873982963*^9}, {
   3.5145713959846573`*^9, 3.5145715810491624`*^9}, 3.514571626092666*^9, {
   3.514571941428197*^9, 3.514571949496003*^9}, {3.514572013747428*^9, 
   3.5145720179048433`*^9}, {3.514572178218873*^9, 3.5145722526373143`*^9}, 
   3.5145723054115915`*^9, {3.5146268730064154`*^9, 3.5146269051263847`*^9}, {
   3.5146270229445705`*^9, 3.5146270681459584`*^9}, {3.5146271812004027`*^9, 
   3.5146272132853527`*^9}, {3.5146273907617598`*^9, 
   3.5146273910437036`*^9}, {3.5146274747225094`*^9, 3.514627478478134*^9}, {
   3.5146275321187696`*^9, 3.5146275322447567`*^9}, {3.5146287653162756`*^9, 
   3.5146288827074027`*^9}, {3.5146289527904105`*^9, 3.514628998029934*^9}, {
   3.514629030344165*^9, 3.5146290851860523`*^9}, {3.5146291435080523`*^9, 
   3.5146292179480524`*^9}, {3.514629299773052*^9, 3.514629386735052*^9}, 
   3.5146294312260523`*^9, 3.514629820341762*^9, 3.514637439340227*^9, {
   3.514637471086774*^9, 3.514637497648658*^9}, {3.514637632286723*^9, 
   3.5146377553183517`*^9}, {3.514637834318388*^9, 3.514637845780695*^9}, {
   3.5146379650319242`*^9, 3.5146379906919975`*^9}, {3.51463810896735*^9, 
   3.5146381098551483`*^9}, {3.5146381522782946`*^9, 
   3.5146382099811807`*^9}, {3.5146382644781837`*^9, 3.514638309677827*^9}, {
   3.514638549999091*^9, 3.514638625849764*^9}, 3.5146387193625264`*^9, {
   3.5148002002850256`*^9, 3.5148002289861507`*^9}, {3.514800284082328*^9, 
   3.514800469299383*^9}, {3.514800542882573*^9, 3.514800554529747*^9}, 
   3.514801036181101*^9, {3.5149178800982714`*^9, 3.5149182402102795`*^9}, {
   3.5149182745977173`*^9, 3.514918378665123*^9}, {3.514924264183536*^9, 
   3.514924285645567*^9}, {3.5149243379256563`*^9, 3.514924338085656*^9}, {
   3.514924392023737*^9, 3.5149246164940634`*^9}, {3.515057409613387*^9, 
   3.515057415973396*^9}, {3.5150576088727074`*^9, 3.5150576473127613`*^9}, {
   3.515057721900881*^9, 3.515057730194895*^9}, 3.515057898669133*^9, {
   3.5150579934712734`*^9, 3.5150579950712757`*^9}, {3.5150580283033237`*^9, 
   3.515058133599481*^9}, {3.515930194980676*^9, 3.5159301951906805`*^9}, {
   3.5159304121609244`*^9, 3.5159304134309373`*^9}, {3.5159304509193144`*^9, 
   3.5159305103759303`*^9}, {3.5162297987780533`*^9, 3.516229799078054*^9}, {
   3.5162299347422504`*^9, 3.516230062028433*^9}, {3.5162301494365625`*^9, 
   3.516230184908616*^9}, {3.516230292814774*^9, 3.51623036598088*^9}, {
   3.5162303992429295`*^9, 3.5162303994629297`*^9}, {3.516230466503023*^9, 
   3.5162304666330233`*^9}, {3.516230508455083*^9, 3.5162305087150836`*^9}, {
   3.516230564513173*^9, 3.5162306450472884`*^9}, {3.516230745471465*^9, 
   3.516230745681465*^9}, {3.516230806321556*^9, 3.5162308918457003`*^9}, {
   3.5162309305277557`*^9, 3.516230930797756*^9}, {3.5162317564852753`*^9, 
   3.5162317702572956`*^9}, {3.5162318590594225`*^9, 3.516231889891469*^9}, {
   3.5162322718500223`*^9, 3.5162323414461308`*^9}, {3.5162324101802335`*^9, 
   3.5162324103902335`*^9}}]
},
WindowSize->{1421, 765},
WindowMargins->{{0, Automatic}, {Automatic, 0}},
FrontEndVersion->"8.0 for Microsoft Windows (32-bit) (February 23, 2011)",
StyleDefinitions->"Default.nb"
]
(* End of Notebook Content *)

(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[545, 20, 15996, 309, 1074, "Input"]
}
]
*)

(* End of internal cache information *)
